package com.dai.controller;


import com.dai.bean.Task;
import com.dai.bean.User;
import com.dai.bean.tasks.TaskZaoWan;
import com.dai.dao.TaskDAO;
import com.dai.dao.UserDAO;
import com.dai.tools.RetMsg;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;

@RestController
public class UserController {

    @Autowired
    private UserDAO userDAO;

    @Autowired
    private TaskDAO taskDAO;

    @GetMapping("/time")
    public RetMsg<Long> getTime(){
        long cur = System.currentTimeMillis();
        return RetMsg.makeSuccessObj(cur);
    }

    @GetMapping("/user/login")
    public RetMsg<User> login(@RequestParam(name = "user_id")int user_id,
                              @RequestParam(name = "sys_bushu") int sys_bushu){
        System.out.println("参数 user_id:" + user_id);
        System.out.println("参数 sys_bushu:" + sys_bushu);

        if (user_id > 0){
            // 存在的用户
            User user = userDAO.getUserByUserId(user_id);
            if(null == user){
                return RetMsg.makeErrObj(RetMsg.Err_No_User,RetMsg.getMsg(RetMsg.Err_No_User));
            }
            // 检查步数
            checkBuShu(user,sys_bushu);

            // 检查需要每日更新的任务
            Task task = taskDAO.getTask(user_id);
            if(task == null){
                // 一般不会到这里,如果到了 直接插入默认数据
                taskDAO.newTask(Task.makeTask(user_id));
            }else{
                // 检查是否需要更新
                String curDayTime = LocalDate.now().toString();
                if(!curDayTime.equals(task.refresh_daytime)){
                    // 不是同一天,需要更新
                    // 怎么更新?
                    // 每天更新的数据 可以直接new
                    // 但是一次性的数据 不可以new
                    // TODO:  分批更新
                    // 从 task中解析出来 重新组装?
                    task.reset();
                    task.refresh_daytime = curDayTime;
                    taskDAO.updateTask(task);
                }
            }

            return  RetMsg.makeSuccessObj(user);
        }else{
            // 当作新用户
            // 新用户 不存在归档
            int newUserId = userDAO.newUser(sys_bushu);
            if(newUserId < 0){
                throw new RuntimeException("插入新用户后,获取其id失败....");
            }

            // 插入默认的任务数据
            // 1. 早晚打卡
            taskDAO.newTask(Task.makeTask(newUserId));

            User user = userDAO.getUserByUserId(newUserId);
            return RetMsg.makeSuccessObj(user);
        }
    }

    @GetMapping("/user/sync")
    public RetMsg<User> sync(@RequestParam(name = "user_id")int userId,
                             @RequestParam(name = "sys_bushu") int sys_bushu){
        // 不考虑请求顺序
        User user = userDAO.getUserByUserId(userId);
        checkBuShu(user,sys_bushu);
        return  RetMsg.makeSuccessObj(user);
    }






    private void checkBuShu(User user,int curSysBushu){
        LocalDate localDate = LocalDate.now();
        if(localDate.toString().equals(user.sync_daytime)){
            // 不需要归档
            boolean needUpdate = refreshUserBushu(user,curSysBushu);
            if(needUpdate){
                // 更新玩家步数
                System.out.println("更新玩家步数");
                userDAO.updateUserBuShu(user);
            }
        }else{
            // 需要进行归档
            refreshUserBushu(user,curSysBushu);

            System.out.println("进行归档");
            userDAO.placeOn(user);
            user.bushu = 0;
            user.sync_daytime = localDate.toString();

            System.out.println("更新玩家步数");
            userDAO.updateUserBuShu(user);
        }
    }

    private boolean refreshUserBushu(User user,int curSysBushu){

        // 认为没有走动过..
        if(user.sys_bushu == curSysBushu){
            return false;
        }

        if(user.sys_bushu > curSysBushu){
            // 重启过
            user.bushu += curSysBushu;
            user.sys_bushu = curSysBushu;
        }else{
            // 没有重启过
            user.bushu += (curSysBushu - user.sys_bushu);
            user.sys_bushu = curSysBushu;
        }
        return  true;
    }
}
